package com.llkj.qingnianguoxue.zhuanlan;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.FragmentAdapter;
import com.llkj.qingnianguoxue.adapter.ZhiBoRechargeAdapter;
import com.llkj.qingnianguoxue.bean.RechargeBean;
import com.llkj.qingnianguoxue.bean.ZhiBoGiftBean;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 礼物弹框工具
 */
public class GiftDialogActivity extends FragmentActivity implements GiftFragment.OnFragmentInteractionListener,ViewPager.OnPageChangeListener,View.OnClickListener {
    protected ImageView ivCloseDialog;//关闭按钮
    protected ViewPager giftPager;//礼物ViewPager
    protected LinearLayout llPageINdicator;//指示器
    protected TextView btnRecharge;//充值按钮
    protected TextView tvBalance;//余额
    protected TextView btnSendGift;//发送礼物按钮
    private FragmentAdapter fragmentAdapter;
    private String mPrice;//价格
    private String mGiftId;//礼物id
    private Dialog mRechargeDialog;//充值弹框

    @BindView(R.id.rvRechargeGrid)
    protected RecyclerView mRechargeGrid;//充值选项
    @BindView(R.id.tvBalance)
    protected TextView mTvRechargeBalance;//充值选项
    private ZhiBoRechargeAdapter mRechargeAdapter;//充值适配器
    private List<RechargeBean> mRechargeData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_zhibo_gift);
        getWindow().setLayout(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        Intent intent = getIntent();
        init();
        initRechargeDialogView();

        List<ZhiBoGiftBean> data = (List<ZhiBoGiftBean>) intent.getSerializableExtra(GiftFragment.GIFT_DATA);
        initGiftData(data);
    }

    private void init(){
        ivCloseDialog = (ImageView) findViewById(R.id.ivClose);
        giftPager = (ViewPager) findViewById(R.id.giftViewPager);
        int height = (DisplayUtil.getDensityWdith(this)-DisplayUtil.dip2px(this,60))/6;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) giftPager.getLayoutParams();
        params.height = height*4+DisplayUtil.dip2px(this,20);

        llPageINdicator = (LinearLayout) findViewById(R.id.llPageIndicator);
        btnRecharge = (TextView) findViewById(R.id.btnRecharge);
        tvBalance = (TextView) findViewById(R.id.tvPrice);
        btnSendGift = (TextView) findViewById(R.id.btnSendGift);

        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        giftPager.setOnPageChangeListener(this);

        ivCloseDialog.setOnClickListener(this);
        btnRecharge.setOnClickListener(this);
        btnSendGift.setOnClickListener(this);
    }

    /**
     * 充值弹框布局
     */
    private void initRechargeDialogView(){
        View rechargeView = LayoutInflater.from(this).inflate(R.layout.dialog_zhibo_recharge,null);
        ButterKnife.bind(this,rechargeView);
        mRechargeDialog = DialogUtil.showDialogBottom(this,rechargeView);

        mRechargeGrid.setLayoutManager(new GridLayoutManager(this, 3));
        mRechargeGrid.addItemDecoration(new GridSpacingItemDecoration(3, DisplayUtil.dip2px(this,5), false));
        mRechargeData = new ArrayList<>();
        initRechargeData();
        mRechargeAdapter = new ZhiBoRechargeAdapter(R.layout.item_master_pay,mRechargeData);
        mRechargeGrid.setAdapter(mRechargeAdapter);

        mRechargeGrid.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mRechargeAdapter.setCheckPosition(i);
            }
        });
    }

    /**
     * 初始数据
     * @param data
     */
    public void initGiftData(List<ZhiBoGiftBean> data){
        int size = data.size()%6==0?data.size()/6:data.size()/6+1;
        List<ZhiBoGiftBean> giftList = null;
        for(int i=0;i<size;i++){
            addIndicatorPoint();
            giftList = new ArrayList<>();
            giftList.addAll(data.subList(i,i+6));
            addGiftFragment(giftList);
        }
        giftList = null;
        tvBalance.setText("0.00银子");
        giftPager.setAdapter(fragmentAdapter);
    }

    /**
     * 礼物ViewPager分页
     */
    private void addGiftFragment(List<ZhiBoGiftBean> giftList){
        GiftFragment giftFragment = GiftFragment.newInstance(giftList);
        giftFragment.setOnFragmentListener(this);
        fragmentAdapter.addFragment(giftFragment);
    }
    /**
     * 增加点
     */
    private void addIndicatorPoint(){
        View pointView = new View(this);
        int margin = DisplayUtil.dip2px(this,5);
        pointView.setBackgroundResource(R.drawable.select_gift_indicator);
        llPageINdicator.addView(pointView);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) pointView.getLayoutParams();
        params.width = DisplayUtil.dip2px(this,8);
        params.height = DisplayUtil.dip2px(this,8);
        params.leftMargin = margin;
        params.rightMargin = margin;
        if(llPageINdicator.getChildCount()==1){
            pointView.setSelected(true);
        }
    }

    @OnClick({R.id.ivCloseRecharge, R.id.btnPay})
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.ivClose://关闭
                finish();
                overridePendingTransition(R.anim.actionsheet_dialog_in, R.anim.actionsheet_dialog_out);
                break;
            case R.id.btnRecharge://充值
                mRechargeDialog.show();
                break;
            case R.id.btnSendGift://发送礼物
                break;
            case R.id.btnPay://充值
                mRechargeDialog.dismiss();
                break;
            case R.id.ivCloseRecharge://关闭充值对话框
                mRechargeDialog.dismiss();
                break;
        }
    }

    /**
     * 选中礼物后回调
     * @param giftId
     */
    @Override
    public void selectGift(String giftId, String price) {
        mPrice = price;
        mGiftId = giftId;
    }

    @Override
    public void onPageSelected(int position) {
        View pointView = null;
        for(int i=0;i<llPageINdicator.getChildCount();i++){
            pointView = llPageINdicator.getChildAt(position);
            if(position==i){
                pointView.setSelected(true);
            }else {
                pointView.setSelected(false);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    @Override
    public void onPageScrollStateChanged(int state) {}

    /**
     * 初始化充值数据
     */
    private void initRechargeData(){
        RechargeBean rechargeBean = new RechargeBean();
        rechargeBean.setCoin("6银子");
        rechargeBean.setPrice("6元");
        mRechargeData.add(rechargeBean);
        rechargeBean = new RechargeBean();
        rechargeBean.setCoin("68银子");
        rechargeBean.setPrice("68元");
        mRechargeData.add(rechargeBean);
        rechargeBean = new RechargeBean();
        rechargeBean.setCoin("208银子");
        rechargeBean.setPrice("208元");
        mRechargeData.add(rechargeBean);
        rechargeBean = new RechargeBean();
        rechargeBean.setCoin("6银子");
        rechargeBean.setPrice("6元");
        mRechargeData.add(rechargeBean);
        rechargeBean = new RechargeBean();
        rechargeBean.setCoin("6银子");
        rechargeBean.setPrice("6元");
        mRechargeData.add(rechargeBean);
        rechargeBean = new RechargeBean();
        rechargeBean.setCoin("6银子");
        rechargeBean.setPrice("6元");
        mRechargeData.add(rechargeBean);
    }
}
